package com.corner.model.cornerservice.domain;

import android.os.Parcel;
import android.os.Parcelable;

import com.alibaba.fastjson.JSONObject;
import com.corner.app.App;
import com.corner.utils.SharePreferenceUtil;
import com.google.gson.annotations.Expose;

public class User extends BaseBean implements Parcelable {
    public static final String USERNAME = "username";
    public static final String AVATER_URL = "avaterUrl";
    public static final String PHONE_NUMBER = "phoneNum";
    public static final String HAS_SET_PASSWORD = "hasSetPassword";
    public static final String INSTALLATION_ID = "installationId";
    public static final String CURRENT_USER = "currentUser";

    private static User currentUser;
    @Expose
    private String username;
    @Expose
    private String password;
    @Expose
    private String phoneNum;
    @Expose
    private CloudFile avatar;
    @Expose
    private String installationId;
    @Expose
    private Boolean hasSetPassword;


    public static User getCurrentUser() {
        if (currentUser == null) {
            String userJson = SharePreferenceUtil.getString(App.application, CURRENT_USER);
            if (userJson != null) {
                currentUser = JSONObject.parseObject(userJson, User.class);
            }
        }
        return currentUser;
    }

    public static void logOut() {
        SharePreferenceUtil.clear(App.application, CURRENT_USER);
        currentUser = null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getInstallationId() {
        return installationId;
    }

    public void setInstallationId(String installationId) {
        this.installationId = installationId;
    }

    public Boolean getHasSetPassword() {
        return hasSetPassword;
    }

    public void setHasSetPassword(Boolean hasSetPassword) {
        this.hasSetPassword = hasSetPassword;
    }

    public CloudFile getAvatar() {
        return avatar;
    }

    public void setAvatar(CloudFile avatar) {
        this.avatar = avatar;
    }

    /***************** 扩展 ***********************/
    /**
     * 头像的路径
     */
    private String avaterPath;
    public String getAvaterPath() {
        return this.avaterPath;
    }
    public void setAvaterPath(String path) {
        this.avaterPath = path;
    }
    public String getAvaterUrl() {
        if (avatar!=null){
            return avatar.getFileUrl();
        }
        return null;
    }
    /**************************************************************************/


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.username);
        dest.writeString(this.password);
        dest.writeString(this.phoneNum);
        dest.writeParcelable(this.avatar, 0);
        dest.writeString(this.installationId);
        dest.writeValue(this.hasSetPassword);
        dest.writeString(this.avaterPath);
    }

    public User() {
    }

    protected User(Parcel in) {
        super(in);
        this.username = in.readString();
        this.password = in.readString();
        this.phoneNum = in.readString();
        this.avatar = in.readParcelable(CloudFile.class.getClassLoader());
        this.installationId = in.readString();
        this.hasSetPassword = (Boolean) in.readValue(Boolean.class.getClassLoader());
        this.avaterPath = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

        public User[] newArray(int size) {
            return new User[size];
        }
    };


    public static void cacheUser(User user) {
        SharePreferenceUtil.saveString(App.application, User.CURRENT_USER, JSONObject.toJSONString(user));
        currentUser = getCurrentUser();
    }
}
